package questionsObjects;

import java.util.*;


/**
 * Created with IntelliJ IDEA.
 * User: xpk
 * Date: 11/14/13
 * Time: 8:09 PM
 * To change this template use File | Settings | File Templates.
 */
public class QuestionManager {
    private Queue<String> themesOfQuestions;
    private List<ComplexityAndPosition> complexityAndPositions;
    private Questions questions;
    private int rightAnswers;
    private int wrongAnswers;
    private int currentComplexity;
    private String currentTheme;
    private int currentIndexOfQuestion;

    public QuestionManager(Questions questions) {
        rightAnswers = 0;
        wrongAnswers = 0;
        currentComplexity = 2;
        currentIndexOfQuestion = 0;
        themesOfQuestions = new LinkedList<String>();
        complexityAndPositions = new ArrayList<ComplexityAndPosition>();
        this.questions = questions;

        prepareThemesOfQuestions();
        prepareMapComplexityAndQuestion();
        displayMessages();
    }

    private void displayMessages() {
        System.out.println("Your theme is " + currentTheme);
        System.out.println("Your current complexity is " + currentComplexity);
    }

    private void prepareThemesOfQuestions() {
        for (int i = 0; i < questions.length(); i++) {
            if (!themesOfQuestions.contains(questions.getTheme(i))) {
                themesOfQuestions.offer(questions.getTheme(i));
            }
        }
        currentTheme = themesOfQuestions.poll();
    }

    private void prepareMapComplexityAndQuestion() {
        for (int i = 0; i < questions.length(); i++) {
            if (questions.getTheme(i).equals(currentTheme)) {
                complexityAndPositions.add(new ComplexityAndPosition(questions.getComplexity(i), i));
            }
        }
    }

    private void prepareDataForNextTheme() {
        currentTheme = themesOfQuestions.poll();
        currentComplexity = 2;
        rightAnswers = 0;
        wrongAnswers = 0;
        complexityAndPositions.clear();

        prepareMapComplexityAndQuestion();
        displayMessages();
    }

    public boolean hasNext() {
        if (rightAnswers == 2 && currentComplexity != 3) {
            ++currentComplexity;
            rightAnswers = 0;
            System.out.println("You are growing up. You have the level " + currentComplexity);
        }
        if (rightAnswers == 2 && currentComplexity == 3) {
            if (themesOfQuestions.peek() != null) {
                prepareDataForNextTheme();
            } else {
                return false;
            }
        }
        if (wrongAnswers == 2 && currentComplexity != 1) {
            --currentComplexity;
            wrongAnswers = 0;
            System.out.println("You are growing down. You have the level " + currentComplexity);
        }
        if (wrongAnswers == 1 && currentComplexity == 1) {
            if (themesOfQuestions.peek() != null) {
                prepareDataForNextTheme();
            } else {
                return false;
            }
        }

        if (!complexityAndPositions.contains(new ComplexityAndPosition(currentComplexity,1))) {
            System.out.println("!complexityAndPositions.contains(currentComplexity)");
            return false;
        }
        return true;
    }

    public String getQuestion() {
        for (ComplexityAndPosition s : complexityAndPositions) {
            if (s.getComplexityOfQuestion() == currentComplexity) {
                currentIndexOfQuestion = s.getPositionOfQuestion();
            }
        }
        return questions.getQuestion(currentIndexOfQuestion);
    }

    public String getAnswers() {
        return questions.getAnswers(currentIndexOfQuestion);
    }

    public boolean isCorrectAnswer(String answer) {
        boolean isCorrectAnswer;
        isCorrectAnswer = questions.isCorrectAnswer(currentIndexOfQuestion, answer);
        if (isCorrectAnswer) {
            System.out.println("Your answer is true");
            ++rightAnswers;
        } else {
            System.out.println("Your answer is false");
            ++wrongAnswers;
        }

        int i = 0;
        for (ComplexityAndPosition s : complexityAndPositions) {
            if(s.getPositionOfQuestion() == currentIndexOfQuestion) {
                break;
            }
            ++i;
        }
        complexityAndPositions.remove(i);
        return isCorrectAnswer;
    }


}
